import config from '../../config/config'
import api from '../request/api'
import Vue from 'vue'
import VueRouter from 'vue-router'
import Page404 from '../../views/404'
Vue.use(VueRouter)

export const routes = []

// 自动读取views文件夹下的所有vue文件
const files = require.context('../../views', true, /\.vue$/);
files.keys().map((item) => {
	
  let path = item.slice(1).replace(".vue", "").toLowerCase();
  const comp = files(item).default;
  if(path == '/index/index'){
	  path = '/'
  }
  // console.log(comp)
  routes.push({
    path,
    name: comp.pageName!=undefined?comp.pageName:'',
    component: comp,
  });
});

// 当匹配不到路由时，匹配一个404页面
routes.push({
  path: '*',
  name: 'Error',
  component:Page404,
})

// console.log(routes)

const router = new VueRouter({
  mode: config.router.routerMode,
  scrollBehavior: () => ({
    y: 0 // 始终滚动到顶部
  }),
  routes
})



//路由守卫
router.beforeEach((to, from, next) => {
	// console.log(to)
  // if (to.path == '/login/login') {
  //   if (localStorage.getItem('admintoken')) {
  //     next('/index/index');
  //   }
  //   next();
  // } else {
  //   if (!localStorage.getItem('admintoken')) {
  //     next('/login/login');
  //   }
  //   next();
  // }
   // 设置网站的图标

   api.ConfigGetConfig({
    key: 'app_down_url',
  }, (res) => {
    if (res.code == 0) {
      let config=res.data[1].value;
      console.log(config,'config')
      const links = document.getElementsByTagName('link');
      for (let i = 0; i < links.length; i++) {
        if (links[i].rel === 'icon') {
          links[i].href = config.domain+config.favicon_logo;
          console.log('更换图标',config.favicon_logo)
        }
      }
     document.title = config.site_title;
    } else {
      // this.$message.error(res.msg);
    }
  })


  next();
})

export default router
